package com.wangdong.wdutils.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author xinsuinizouyuan
 *
 */
public class DLViewHolder {

	private View mCurrentView;//当前行对象
	private int mPosition;//当前行的position
	private SparseArray<View> views;//当前行对象上的所有控件
	private Context mContext;

	public DLViewHolder(Context mContext, int position, ViewGroup parent, int layoutRes) {
		this.mPosition = position;
		this.mContext = mContext;
		this.mCurrentView = LayoutInflater.from(mContext).inflate(layoutRes, parent, false);
		this.views = new SparseArray<>();
		this.mCurrentView.setTag(this);
	}

	public static DLViewHolder get(Context mContext, int position, View convertView, ViewGroup parent, int layoutRes) {
		DLViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new DLViewHolder(mContext, position, parent, layoutRes);
		} else {
			viewHolder = (DLViewHolder) convertView.getTag();
			viewHolder.mPosition = position;
		}
		return viewHolder;
	}

	public <T extends View> T getView(int viewId){    //R.id.iv_head
		View view = views.get(viewId);
		if(view == null){
			view = mCurrentView.findViewById(viewId);
			views.put(viewId,view);
		}
		return (T) view;
	}

	//4:
	public View getCurrentView() {
		return mCurrentView;
	}
	public int getPosition() {
		return mPosition;
	}

	/**
	 * 设置TextView的值
	 *
	 * @param viewId
	 * @param text
	 * @return
	 */
	public DLViewHolder setText(int viewId, String text) {
		TextView tv = getView(viewId);
		tv.setText(text);
		return this;
	}

	public DLViewHolder setText(int viewId,CharSequence text){
		TextView tv = getView(viewId);
		tv.setText(text);
		return this;
	}

	public DLViewHolder setVisiblity(int viewId) {
		TextView tv = getView(viewId);
		tv.setVisibility(View.VISIBLE);
		return this;
	}

	public DLViewHolder setWhetherVisiblity(int viewId, int number) {
		TextView tv = getView(viewId);
		if (number == 1) {
			tv.setVisibility(View.VISIBLE);
		}else {
			tv.setVisibility(View.INVISIBLE);
		}
		return this;
	}


	public DLViewHolder setGone(int viewId) {
		View tv = getView(viewId);
		tv.setVisibility(View.GONE);
		return this;
	}

	public DLViewHolder setInvisible(int viewId){
		View tv = getView(viewId);
		tv.setVisibility(View.INVISIBLE);
		return this;
	}
	public DLViewHolder setVisible(int viewId) {
		View tv = getView(viewId);
		tv.setVisibility(View.VISIBLE);
		return this;
	}
	public DLViewHolder setImageResource(int viewId, int resId) {
		ImageView view = getView(viewId);
		view.setImageResource(resId);
		return this;
	}

	public DLViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
		ImageView view = getView(viewId);
		view.setImageBitmap(bitmap);
		return this;
	}

	public DLViewHolder setImageDrawable(int viewId, Drawable drawable) {
		ImageView view = getView(viewId);
		view.setImageDrawable(drawable);
		return this;
	}

	public DLViewHolder setBackgroundColor(int viewId, int color) {
		View view = getView(viewId);
		view.setBackgroundColor(color);
		return this;
	}

	public DLViewHolder setBackgroundRes(int viewId, int backgroundRes) {
		View view = getView(viewId);
		view.setBackgroundResource(backgroundRes);
		return this;
	}

	public DLViewHolder setTextColor(int viewId, int textColor) {
		TextView view = getView(viewId);
		view.setTextColor(textColor);
		return this;
	}

	public DLViewHolder setTextColorRes(int viewId, int textColorRes) {
		TextView view = getView(viewId);
		view.setTextColor(mContext.getResources().getColor(textColorRes));
		return this;
	}

	public DLViewHolder setTag(int viewId, Object tag) {
		View view = getView(viewId);
		view.setTag(tag);
		return this;
	}

	public DLViewHolder setTag(int viewId, int key, Object tag) {
		View view = getView(viewId);
		view.setTag(key, tag);
		return this;
	}

	/**
	 * 关于事件的
	 */
	public DLViewHolder setOnClickListener(int viewId,
										 View.OnClickListener listener) {
		View view = getView(viewId);
		view.setOnClickListener(listener);
		return this;
	}

	public DLViewHolder setOnTouchListener(int viewId,
										 View.OnTouchListener listener) {
		View view = getView(viewId);
		view.setOnTouchListener(listener);
		return this;
	}

	public DLViewHolder setOnLongClickListener(int viewId,
											 View.OnLongClickListener listener) {
		View view = getView(viewId);
		view.setOnLongClickListener(listener);
		return this;
	}
}
